<template>
	<view class="">
		<view class="background">
			<u-navbar :border-bottom="false" :title="data.title||data.name" title-color="#fff"
				:background="$background" />
			<view class="h100"></view>
		</view>
		<view class="p20 pr" style="margin-top: -70rpx;">
			<schedule :length="0" />
			<block v-if="data.master">
				<view class="h20"></view>
				<view class="flex p20 bg-fff radius20">
					<u-avatar :src="data.master.avatar||''" size="110" :showSex="!!data.master.sex"
						@click="$tool.lookImg([data.master.avatar||''])"
						:sex-icon="data.master.sex == 2 ? 'man' : data.master.sex == 1 ? 'woman' : 'account'" />
					<view class="fs28 fw-700 u-line-1 ml20">{{data.master.name}}</view>
					<view class="ml10" v-if="data.master.is_name_auth==1">
						<image src="/static/img/index/ysm.png" class="w116 h36 db " mode=""></image>
					</view>
					<view class="flex1"></view>
				</view>
			</block>
			<view class="h20"></view>
			<view class="flex radius20 bg-fff p20 row-between" @click="$jump('/pages/index/demo/detail',$params)">
				<view class="fw-w">服务须知</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
			<u-form ref="uForm" label-width="auto" :label-style="{color: '#666'}">
				<view class="mt20 bg-fff radius20 pl20 pr20">
					<block v-for="(item,index) in list" :key="index">
						<view v-if="!item.hide" :class="item.class||''">

							<block v-if="item.field=='drugs'">
								<view class="pl10" v-for="(item2,index2) in item.child" :key="index2">
									<view class="flex row-between pt30 pb10">
										<view class="color-999">药品{{index2+1}}</view>
										<view class="flex col-bottom" style="color: #FF4444;" v-if="item.child.length>1"
											@click="click(item.childDel,item,index2)">
											<u-icon name="minus-circle-fill" size="34" click="db" />
											<view class="ml10">删除药品</view>
										</view>
									</view>
									<u-form-item v-for="(item3,index3) in item2" :key="index3" :label="item3.name">
										<u-input v-model="item3.value" :placeholder="'填写'+item3.name"
											inputAlign="right" />
									</u-form-item>
								</view>
								<view class="flex col-bottom u-border-bottom pb20 pt20" style="color: #FF704B;"
									@click="click(item.drugs_item,item)">
									<u-icon name="plus-circle-fill" size="34" class="db" />
									<view class="ml10">添加药品</view>
								</view>
							</block>

							<u-form-item v-else :label="item.formName" :borderBottom="item.borderBottom"
								:rightIcon="item.right?'arrow-right':''" :labelPosition="item.labelPosition||'left'">
								<view class="flex w-max" :class="{'row-right':item.inputAlign!='left'}"
									v-if="item.type=='img'">
									<z-imgs up :width="item.width" :height="item.height" :size="item.size||180"
										:upSrcImg="item.src" :max="item.max||1" :hideBor="item.hideBor"
										@success="setImg(item,$event)" />
									<view class="color-fs fs28 text-vertical ml20" v-if="item.shili"
										@click="$tool.lookImg([item.shiliSrc])">示 例</view>
								</view>
								<view class="flex w-max" :class="{'row-right':item.inputAlign!='left'}"
									v-else-if="item.type=='radio'">
									<view class="ml20"
										@click="item.value!==item2.id?click(item.radioClick,item,item2):''"
										v-for="(item2,index2) in item.list" :key="index2">
										<view class="pe-none">
											<radio :checked="true" v-if="item.value===item2.id" class="scale7" />
											<radio :checked="false" v-else class="scale7" /><text>{{item2.name}}</text>
										</view>
									</view>
								</view>
								<view class="flex w-max" v-else-if="item.type=='expect_time'">
									<view class="flex1"></view>
									<view class="">
										<uni-datetime-picker @change="item.value=$event" hide-second
											:start="data.min_timestamp" :end="data.max_timestamp"
											:modelValue="data.min_timestamp">
											<view class="" v-if="item.value">{{item.value}}</view>
											<view class="color-999" v-else>请选择时间</view>
										</uni-datetime-picker>
									</view>
								</view>
								<u-input v-else :inputAlign="item.inputAlign||'right'" :clearable="false"
									:type="item.inputType||'text'" :selectOpen="item.listShow||item.popup"
									v-model="item.value" :placeholder="item.placeholder"
									:disabled="item.disabled||false" :maxlength="item.maxlength||100"
									:border="item.inputBorder||false" @click="click(item.click,item)"
									:backgroundColor="item.itemBg||''" @input="click(item.input,item)"
									@blur="click(item.blur,item)" />

							</u-form-item>

							<block v-if="item.type=='select'">
								<u-select v-model="item.listShow" :list="item.list||[]" @confirm="confirm(item,$event)"
									:title="item.placeholder" mode="mutil-column-auto" :num="item.listNum||''"
									:labelName="item.labelName||'label'" :childName="item.childName||'child'"
									:valueName="item.valueName||'value'" />
							</block>

							<block v-if="item.type=='time'">
								<u-picker mode="time" v-model="item.listShow" :params="item.timeParams"
									@confirm="confirmPicker(item,$event)" />
							</block>

							<block v-if="item.field=='estimate_price'">
								<u-popup v-model="item.popup" mode="bottom">
									<view class="">
										<view class="flex p30 border-b">
											<view class="w200"></view>
											<view class="flex1 fw-w fs32 tc">药品保价</view>
											<view class="w200 tr color-999" @click="item.popup=false">取消</view>
										</view>
										<view class="p30">
											<view class="fw-w fs30 mb30">药品投保价值（元 ）</view>
											<u-input border backgroundColor="#f5f5f5" v-model="item.value0"
												type="number" :placeholder="item.pop_placeholder"></u-input>
											<!-- <view class="mt30">保费金额：0元</view> -->
											<view class="mt30 mb20 fs26 color-666">保价说明</view>
											<u-parse :html="data.insured_tips" :domain="$baseUrl"></u-parse>
											<view class="flex fs26 mt30" @click="item.checked=!item.checked">
												<view class="pe-none">
													<radio :checked="true" v-if="item.checked" class="scale7" />
													<radio :checked="false" v-else class="scale7" />
												</view>
												<view class="">我已阅读并同意</view>
												<view class="color-zs"
													@click.stop="$goText({detail:data.insured_pact,tit:'报价协议'})">《报价协议》
												</view>
											</view>
											<view class="mt30">
												<u-button type="warning" :disabled="!item.checked"
													@click="click(item.confirm,item)">确认</u-button>
											</view>
										</view>
									</view>
								</u-popup>
							</block>
						</view>

					</block>
				</view>
				<view class="mt20 bg-fff radius20 p20">
					<view class="color-fs">注意事项：</view>
					<view class="color-666 mt20 fs26 lh15">
						<text>{{data.note}}</text>
					</view>
				</view>
			</u-form>
			<view class="h180"></view>
			<view class="fixed left0 bottom0 bg-fff p20 w-max z-index2 pt20 pb10">
				<view class="flex row-center" @click="is=!is,$setData('downOrder_is',is)">
					<radio :checked="is" class="pe-none scale7" :color="$zs" />
					<view class="color-666 fs26" id="is">我已认真阅读
						<text class="color-zs"
							@click.stop="$jump('/pages/public/text',{text:'service_pact',tit:'服务条款同意书'}),is=true">《服务条款同意书》</text>
					</view>
				</view>
				<view class="h20"></view>
				<u-button type="warning" ripple @click="submit">
					<text v-if="price.suggested_price">￥{{price.suggested_price}}</text>
					<text>立即下单</text>
				</u-button>
			</view>
		</view>
		<u-popup mode="center" v-model="noteShow" width="600" borderRadius="20">
			<view class="p40 flex-col col-center">
				<view class="fs32 fw-w pb20">注意事项</view>
				<scroll-view scroll-y class="fs30" style="min-height: 80rpx;max-height: 60vh;">
					<text>{{data.note}}</text>
				</scroll-view>
				<view class="h40"></view>
				<view class="w400">
					<u-button type="warning" ripple @click="noteShow=!noteShow">知道了</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				noteShow: false,
				is: true,


				data: '',
				list: [],
				price: '',

				minTime: new Date().getTime() + 86400 * 1000

			};
		},
		onLoad(e) {
			// let value = 1
			// let aa = (value ?? '') || 333
			// console.log(aa)
			// e.type = e.type || 1
			// if (e.organ_id || e.master_id) e.type = 2
			this.orderInfo()
			this.is = this.$getData('downOrder_is') || false
		},
		methods: {
			empty(value) {
				return value ?? ''
			},

			submit() {
				if (!this.is) return this.$msg('请同意协议')
				let form = {
					...this.$params,
				}
				let aa = this.list.find(item => {
					let value = (item.value_id ?? '') || item.value
					if (item.child) {
						value = []
						return item.child.find((item2, index2) => {
							value[index2] = {}
							return item2.find(item3 => {
								value[index2][item3.field] = item3.value
								if (item3.value === '') {
									item.warning = '请填写' + item.name + (index2 + 1) + item3.name
									return true
								}
							})
						})
					}
					form[item.field] = value
					return item.is_must == 1 && !item.hide && (value ?? '') === ''
				})
				console.log(form);
				if (aa) return this.$msg(aa.warning || aa.placeholder)
				this.$request('/member/index/createOrder', form).then(res => {
					let data = res.data
					if (data.service_type == 0 || data.service_type == 1) {
						this.$msg(res.msg)
						this.$setData('orderList', '0')
						setTimeout(() => {
							this.$jump('/pages/order/order', data)
						}, 1000)
						return
					}
					this.$jump('/pages/index/demo/pay', data)
				})
			},
			orderInfo() {
				this.$request('/member/index/orderInfo', {
					master_id: this.$getData('masterid'),
					...this.$params,
				}).then(res => {
					this.dataInit(res.data)
				})
			},
			dataInit(data) {
				if (data.note) this.noteShow = true
				this.data = data
				this.$tool.setTit(data.title || data.name)
				let list = data.items.map(res => {
					let item = this.$tool.deepClone(res)
					switch (item.field) {
						case "medical_history":
							item.inputType = 'textarea'
							item.labelPosition = "top"
							item.inputAlign = "left"
							break;
						case "master_sex": //陪诊师性别
							item.type = 'select'
							item.list = [{
								label: '不限男女',
								value: '0'
							}, {
								label: '男',
								value: 1
							}, {
								label: '女',
								value: 2
							}]
							if (data.master?.sex) { //默认性别
								item.value_id = data.master.sex
								item.value = item.list.find(e => e.value == item.value_id)?.label
								item.hide = true
							}
							break;
						case 'drugs': //药品数组
							item.child = []

							item.drugs_item = (item2) => { //添加
								let drugs_item = [{
										field: 'drugs_name',
										name: '名称',
										value: '',
									},
									{
										field: 'drugs_factory',
										name: '企业',
										value: '',
									},
									{
										field: 'drugs_specs',
										name: '规格',
										value: '',
									},
									{
										field: 'drugs_num',
										name: '数量',
										value: '',
									},
								]
								item2.child.push(JSON.parse(JSON.stringify(drugs_item)))
							}

							item.childDel = (item2, index2) => { //删除
								let aa = item2.child?.[index2].find(e => e.value) || ''
								if (aa) {
									this.$showModal('删除').then(e => item2.child.splice(index2, 1))
								} else {
									item2.child.splice(index2, 1)
								}
							}
							item.drugs_item(item) //先添加一项
							break;
						case 'patient_id': //患者
							item.right = true
							item.disabled = true
							item.click = (item2) => {
								uni.$off('set_patient')
								uni.$once('set_patient', data => {
									item2.value_id = data.id
									item2.value = data.name
								})
								this.$jump('/pages/my/addPatient/patientList', {
									...this.$params,
									set: 'set_patient'
								})
							}
							break;
						case "relation": //陪诊关系
							item.type = 'select'
							item.list = data.relation_arr.map(label => {
								return {
									label,
								}
							})
							break;
						case 'hospital_id': //医院
							item.type = 'select'
							item.front_click = this.getHospital //弹出前
							item.confirm = (item2) => { //确认后
								this.getSuggestedPrice() //获取价钱
								let info = this.getItem('department')
								if (info && this.$config.is_bind_hospital == 1) { //确认后 重置科室
									info.list = []
									info.value = ''
									info.value_id = ''
								}
							}
							if (this.$params.hospital_id) item.class = "pe-none"
							break;
						case 'department': //"科室"
							item.type = 'select'
							item.front_click = this.getDep
							item.listNum = item.listNum || 2
							break;
						case 'expect_time':
							item.type = 'expect_time'
							// item.value=this.minTime
							break;
						case 'mobile':
							item.inputType = 'number'
							item.maxlength = 11
							item.value = this.$user.mobile
							break;
						case 'remarks': //服务需求
							item.inputType = 'textarea'
							item.labelPosition = "top"
							item.inputAlign = "left"
							item.borderBottom = false
							item.inputBorder = true
							item.itemBg = '#f8f8f8'
							break;
						case 'images': //上传图片
							item.type = 'img'
							item.size = 150
							item.max = item.max || 3
							item.labelPosition = "top"
							item.inputAlign = "left"
							item.formName = ""
							break;
						case 'address_id': //设置地址
							item.right = true
							item.disabled = true
							item.click = (item2) => { //设置地址
								uni.$off('set_address')
								uni.$once('set_address', data => {
									item2.value_id = data.id
									item2.value = data.address
								})
								this.$jump('/pages/my/address/address', {
									...this.$params,
									set: 'set_address'
								})
							}
							break;
						case 'qr_code':
							item.type = 'img'
							item.shili = true
							item.shiliSrc = '/static/img/demo/1.png'
							break;
						case 'bar_code':
							item.type = 'img'
							item.shili = true
							item.shiliSrc = '/static/img/demo/2.png'
							break;
						case 'report_type':
							item.type = 'select'
							item.list = data.report_type_arr?.map(label => {
								return {
									label
								}
							}) || []
							break;
						case 'drugs_type':
							item.type = 'select'
							item.list = [{
								label: '非处方药',
								value: '0'
							}, {
								label: '处方药',
								value: 1
							}]
							break;
						case 'prescription':
							item.type = 'img'
							item.size = 150
							break;
						case "expect_price": //期望价格
							item.placeholder = '建议价格' + data.suggested_price
							break;
						case "is_invoice": //需要发票//1是，0是
							item.type = 'radio' //单选
							item.value = 0
							item.list = [{
								name: '是',
								id: 1
							}, {
								name: '否',
								id: 0
							}]
							break;
						case "is_insured": //贵重药品保价服务
							item.type = 'radio' //单选
							item.value = 0
							item.list = [{
								name: '开启',
								id: 1
							}, {
								name: '关闭',
								id: 0
							}]
							item.change = item1 => {
								let insured_price = this.getItem('insured_price')
								if (insured_price) {
									insured_price.hide = item1.value === 0 ? true : false
									if (insured_price.value) this.getSuggestedPrice()
								}

								let estimate_price = this.getItem('estimate_price')
								if (estimate_price) estimate_price.hide = item1.value === 0 ? true : false

							}
							break;
						case 'estimate_price': //预估费用
							item.hide = true
							// item.right = true
							item.inputType = 'select'
							item.popup = false
							item.disabled = true
							item.click = (item1) => {
								console.log(item1);
								item1.popup = true
							}
							item.value0 = ''
							item.checked = false
							item.confirm = (item1) => {
								if (!item.value0) return this.$msg('请输入报价')
								if (item.value0 > data.max_insured_price * 1) return this.$msg('最高报价' + data
									.max_insured_price)
								if (item.value0 < data.min_insured_price * 1) return this.$msg('最低报价' + data
									.min_insured_price)
								item1.value = item.value0
								this.getSuggestedPrice()
								item1.popup = false
							}
							item.placeholder = '请设置' + item.name
							item.pop_placeholder = '最高报价' + data.max_insured_price
							break;
						case "insured_price": //保费
							item.hide = true
							item.disabled = true
							item.placeholder = '请设置保价金额后计算保费'
							break;
						case 'register_type': //挂号类型
							item.type = 'radio' //单选
							item.value = 0
							item.list = [{ // 0普通号 1专家号
									id: 0,
									name: '普通号',
								},
								{
									id: 1,
									name: '专家号',
								},
							]
							item.change = item1 => {
								let info = this.getItem('doctor')
								if (info) info.hide = item1.value === 0 ? true : false
							}
							break;
						case 'doctor': //"专家"名称
							item.hide = true
							break;
						case 'date': //
							item.type = 'time' //单选
							item.timeParams = {
								year: true,
								month: true,
								day: true,
								hour: false,
								minute: false,
								second: false
							}
							break;
						case 'start_time': //开始时间
							item.type = "time"
							item.timeParams = {
								year: false,
								month: false,
								day: false,
								hour: true,
								minute: true,
								second: false
							}
							item.confirm = this.getSuggestedPrice //更新价格
							break;
						case 'end_time': //结束时间
							item.type = "time"
							item.timeParams = {
								year: false,
								month: false,
								day: false,
								hour: true,
								minute: true,
								second: false
							}
							item.confirm = this.getSuggestedPrice //更新价格
							break;
						case 'start_date': //开始日期
							item.type = "time"
							item.timeParams = {
								year: true,
								month: true,
								day: true,
								hour: false,
								minute: false,
								second: false
							}
							item.confirm = this.getSuggestedPrice //更新价格
							break;
						case 'end_date': //结束日期
							item.type = "time"
							item.timeParams = {
								year: true,
								month: true,
								day: true,
								hour: false,
								minute: false,
								second: false
							}
							item.confirm = this.getSuggestedPrice //更新价格
							break;
						case 'is_transfer': //接送服务
							item.type = 'radio' //单选
							item.value = 0
							item.list = [{
								name: '关闭',
								id: 0
							}, {
								name: '开启',
								id: 1
							}]
							item.change = item1 => {
								console.log(item1);
								let info = this.getItem('transfer_id')
								if (info) info.hide = item1.value === 0 ? true : false
							}
							break;
						case 'transfer_id': //接送服务地址
							item.hide = true
							item.click = (item2) => {
								uni.$off('set_transfer')
								uni.$once('set_transfer', data => {
									item2.value_id = data.id
									item2.value = data.address
								})
								this.$jump('/pages/my/address/address', {
									set: 'set_transfer'
								})
							}
							item.right = true
							item.disabled = true
							break;
						case 'is_trade': //是否允许转单
							item.type = 'radio'
							item.value = 1
							break;

						default:
							break;
					}

					switch (item.type) {
						case 'radio':
							item.list = item.list || [{
								name: '否',
								id: 0
							}, {
								name: '是',
								id: 1
							}]
							item.value = item.value ?? item.list[0].id
							item.radioClick = (item1, item2) => {
								item1.value = item2.id
								item.change && item.change(item1, item2)
							}
							break;
						case 'select':
							item.inputType = 'select'
							item.listShow = item.listShow ?? false
							item.list = item.list || []
							item.click = async (item2) => {
								let aa = true
								if (item2.front_click) aa = await item2.front_click(item2) //弹出前
								if (!item2.list.length) return this.$msg('无数据')
								if (aa) item2.listShow = true
							}
							break;
						case 'time':
							item.inputType = 'select'
							item.timeParams = item.timeParams || {
								year: true,
								month: true,
								day: true,
								hour: true,
								minute: true,
								second: false
							}
							item.listShow = false
							item.click = item2 => item2.listShow = !item2.listShow
							break;
						default:
							break;
					}



					//默认
					item.type = item.type || '' //表单类型 //radio
					item.value = item.value ?? '' //表单v-mode
					item.formName = item.formName ?? item.name ?? '' //
					item.value_id = item.value_id ?? '' //优先取
					//u-select组件参数
					// item.labelName = item.labelName ?? 'label'
					// item.childName = item.childName ?? 'child'
					// item.valueName = item.valueName ?? 'value'
					//end
					item.click = item.click ?? function() {}
					if (!item.placeholder) {
						if (item.disabled || item.inputType == 'select') item.placeholder = '请选择' + item.name
						item.placeholder = item.placeholder ?? '请输入' + item.name
					}

					Object.keys(res).map(e => item[e] = res[e]) //最终取接口数据

					return item
				})
				// let handler = {
				// 	get: function(obj, prop) {
				// 		const v = Reflect.get(obj, prop);
				// 		if (v !== null && typeof v === 'object') {
				// 			return new Proxy(v, handler); //代理内层
				// 		} else {
				// 			return v; // 返回obj[prop]
				// 		}
				// 	},
				// 	set: (obj, prop, value) => {
				// 		console.log(obj, prop, value);
				// 		if (obj.upPrice && prop == 'value') {
				// 			this.getSuggestedPrice()
				// 		}
				// 		return trur//设置成功返回true
				// 		return Reflect.set(obj, prop, value); //设置成功返回true
				// 	}
				// };
				// list = new Proxy(list, handler);
				console.log(list);
				this.list = list
				// console.log(this.list);
			},

			click(fun, ...item2) {
				fun && fun(...item2)
			},

			getItem(field) { //获取某一项
				if (!field) return ''
				let index = this.list.findIndex(e => e.field == field) || 0
				return this.list[index] || ''
			},
			confirmPicker(item, e) { //时间
				let value = ''
				if (e.year) value += e.year
				if (e.month) value += '-' + e.month
				if (e.day) value += '-' + e.day
				if (e.hour) value += ' ' + e.hour
				if (e.minute) value += ':' + e.minute
				item.value = value
				item.confirm && item.confirm(item) //确认完回调
			},
			setImg(item, ids) { //设置图片
				item.value = ids
			},
			confirm(item, event) { //列表选择其中一项
				let arr = event.reverse()
				let aa = arr.find(it => it.label)
				item.value = aa.label
				item.value_id = aa.value || ''
				item.confirm && item.confirm(item) //确认完回调
			},
			async getHospital(item, listShow = true) { //获取医院
				if (item.list.length) return listShow
				let params = {
					type: '1',
					...this.$params,
				}
				return await this.$request('/master/commom/getHospital', params).then(res => {
					item.hospital = res.data
					item.list = res.data.map(item => {
						let aa = {
							label: item.name,
							value: item.id,
						}
						return aa
					})
					return true
				})
			},
			async getDep(item) { //获取科室
				if (item.list.length) return true
				let hospital = this.getItem('hospital_id')
				return await this.$request('/master/commom/getDepartment', {
					...this.$params,
					hospital_id: hospital?.value_id || "",
				}).then(res => {
					item.list = res.data.data || []
					return true
				})
			},
			getForm(getForm) {
				let form = {
					...this.$params,
				}
				this.list.map(item => {
					let value = (item.value_id ?? '') || item.value
					if (item.child) {
						value = item.child.map(item2 => {
							let aa = {}
							item2.map(it3 => aa[it3.field] = it3.value)
							return aa
						})
					}
					form[item.field] = value
					if (item.is_must == 1 && !item.hide && (value ?? '') === '') return true
				})
				return form
			},
			getSuggestedPrice() { //获取价格
				let form = this.getForm()
				this.$request('/master/commom/getSuggestedPrice', {
					...this.$params,
					...form,
				}).then(res => {
					let data = res.data || ''
					this.price = data
					if (data.insured_price) {
						let insured_price = this.getItem('insured_price')
						if (insured_price) insured_price.value = data.insured_price
					}
				})
			},
		},
	}
</script>

<style lang="scss">
	.line {
		width: 70rpx;
		height: 0rpx;
		border: 1rpx dashed #72dbde;
	}

	.-right-10 {
		right: -35rpx;
	}

	.pb120 {
		padding-bottom: 200rpx;
	}
</style>